package com.xuelang.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.xuelang.hiforce.base.tracing.TracingUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * MybatisPlus配置类
 *
 * @author xiwen.zxg
 * @since 2024/5/16
 */
@Configuration
@MapperScan(basePackages = "com.xuelang.pipeline.infrastructure.*.mapper")
public class MybatisPlusConfig {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 动态设置 DbType
        DbType dbType = determineDbType();  // 自定义方法，根据实际环境判断当前数据库类型
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType));

        return interceptor;
    }

    private DbType determineDbType() {
        // 根据当前数据源或其他逻辑判断数据库类型
        String url = dataSourceProperties.getUrl();  // 获取当前数据源的 URL

        if (url.contains("mysql")) {
            return DbType.MYSQL;
        } else if (url.contains("dm")) {
            return DbType.DM;
        }
        // 其他数据库类型判断逻辑
        return DbType.MYSQL;  // 默认返回 MySQL
    }

    /**
     * 自定义公共字段自动注入
     *
     * @author yishou.lys
     * @date 2024/6/6 16:42
     */
    @Component
    public static class CustomMetaObjectHandler implements MetaObjectHandler {

        /** 删除标志 */
        private static final String DELETE_FLAG = "deleteFlag";

        /** 创建人 */
        private static final String CREATE_USER = "creatorId";

        /** 创建时间 */
        private static final String CREATE_TIME = "createTime";

        /** 更新人 */
        private static final String UPDATE_USER = "updaterId";

        /** 更新时间 */
        private static final String UPDATE_TIME = "updateTime";

        @Override
        public void insertFill(MetaObject metaObject) {
            try {
                //为空则设置createUser
                Object createUser = metaObject.getValue(CREATE_USER);
                if (ObjectUtil.isNull(createUser)) {
                    setFieldValByName(CREATE_USER, TracingUtils.getUserId(), metaObject);
                }
            } catch (ReflectionException ignored) { }
            try {
                //为空则设置createTime
                Object createTime = metaObject.getValue(CREATE_TIME);
                if (ObjectUtil.isNull(createTime)) {
                    setFieldValByName(CREATE_TIME, DateTime.now(), metaObject);
                }
            } catch (ReflectionException ignored) { }
        }

        @Override
        public void updateFill(MetaObject metaObject) {
            try {
                //设置updateUser
                setFieldValByName(UPDATE_USER, TracingUtils.getUserId(), metaObject);
            } catch (ReflectionException ignored) { }
            try {
                //设置updateTime
                setFieldValByName(UPDATE_TIME, DateTime.now(), metaObject);
            } catch (ReflectionException ignored) { }
        }

    }
}